<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="90px" @submit.native.prevent>
      <el-form-item label="发音人名称" prop="name">
        <el-input
          v-model="queryParams.name"
          placeholder="请输入发音人名称"
          clearable
          @clear="handleQuery"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['human:speaker:add']"
        >新增</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="speakerList">
      <el-table-column label="序号" type="index" fixed></el-table-column>
      <el-table-column label="发音人名称" align="center" prop="name" />
      <el-table-column label="描述" align="center" prop="remark" />
      <el-table-column label="语速 | 音量 | 音调" align="center" prop="soundInfo" />
      <el-table-column label="创建时间" align="center" prop="createTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.createTime)}}</span>
        </template>
      </el-table-column>
      <el-table-column label="状态" align="center" width="100" prop="status">
        <template slot-scope="scope">
          <el-switch
            v-model="scope.row.status"
            :active-value="1"
            :inactive-value="0"
            disabled
          ></el-switch>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['human:speaker:edit']"
          >编辑</el-button>
          <el-button
            size="mini"
            type="text"
            @click="handleDelete(scope.row)"
            v-hasPermi="['human:speaker:remove']"
          >删除</el-button>
          <el-button
              size="mini"
              type="text"
              @click="updateConfigStatus(scope.row)"
              v-hasPermi="['human:speaker:query']"
          >
            <span v-show="parseInt(scope.row.status) == 1">停用</span>
            <span v-if="parseInt(scope.row.status) == 0">启用</span>
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />
    <!-- 添加或修改发音人对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="50%" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" @submit.native.prevent>
        <el-row>
          <el-col :span="18">
            <el-col :span="16">
              <el-form-item label="发音人名称:" prop="name" label-width="110px">
                <el-input v-model="form.name" size= "small" placeholder="请输入发音人名称"/>
              </el-form-item>
            </el-col>
          </el-col>
          <el-col :span="6">
            <el-form-item label="状态:" prop="status" label-width="90px">
              <el-switch
                v-model="form.status"
                :active-value="1"
                :inactive-value="0"
              >
              </el-switch>
            </el-form-item>
          </el-col>
          <el-col :span="24">
            <el-form-item label="类别:" prop="styleUid" label-width="110px">
              <el-select v-model="form.styleUid" placeholder="请选择类别">
                <el-option
                  v-for="item in this.speakerTypeList"
                  :key="item.uid"
                  :label="item.styleName"
                  :value="item.uid">
                </el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="24">
            <el-form-item label="参数:" prop="param" label-width="110px">
              <el-input type= "textarea" v-model="form.param" :rows="4"></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="24" class="sound_info">
            <el-col :span="16">
              <el-form-item label="语速:" prop="speed" label-width="110px">
                <el-slider v-model="form.speed" input-size="small"></el-slider>
              </el-form-item>
              <el-form-item label="音量:" prop="soundVolume" label-width="110px">
                <el-slider v-model="form.soundVolume" input-size="small"></el-slider>
              </el-form-item>
              <el-form-item label="音调:" prop="soundTone" label-width="110px">
                <el-slider v-model="form.soundTone" input-size="small"></el-slider>
              </el-form-item>
            </el-col>
          </el-col>
          <el-col :span="24">
            <el-form-item label="描述信息:" prop="remark" label-width="110px">
              <el-input type= "textarea" v-model="form.remark" :rows="4"></el-input>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <div slot="footer" class="dialog-footer" style="margin-top: -50px">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {listSpeakerInfo, insertSpeakerInfo, selectSpeakerInfo, deleteSpeakerInfo, updateSpeakerInfoStatus, updateSpeakerInfo} from "@/api/platform/human/speaker";
import {listSpeakerTypeInfo} from "@/api/platform/human/speakerType";

export default {
  name: "SpeakerInfo",
  data() {
    let checkParam = (rule, value, callback) => {
      try {
        if(value != ""){
          let obj = JSON.parse(value)
          if(typeof obj == 'object' && obj){
            callback();
          }
        }
        callback();
      }catch(error){
        return callback(new Error('接口参数格式错误'));
      }
    };
    return {
      // 遮罩层
      loading: true,
      // 显示搜索条件
      showSearch: true,
      speakerList: [],
      speakerTypeList: [],
      // 总条数
      total: 0,
      // 弹出层标题
      title: "",
      detailOpen: false,
      // 是否显示添加修改弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
      },
      // 表单参数
      form: {
      },
      // 表单校验
      rules: {
        name: [
          { required: true, message: "应用名称不能为空", trigger: "blur" }
        ],
        param: [
          { validator: checkParam, trigger: "blur" },
          { required: false, message: "接口参数信息不能为空", trigger: "blur" }
        ],
        styleUid:[
          { required: true, message: "发音人类别不能为空", trigger: "blur" }
        ]
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询模板列表 */
    getList() {
      this.loading = true;
      listSpeakerInfo(this.queryParams).then(response => {
        this.speakerList = response.rows;
        this.total = response.total;
        this.loading = false;
      })
    },
    statusFormat(row, index){
      if (row.status === 0) {
        return "未激活";
      } else if(row.status === 1){
        return "激活";
      }
    },
    //修改配置状态
    updateConfigStatus(row){
      let message = ""
      let data = {};
      data.status = row.status;
      data.uid = row.uid
      if(data.status == 0){
        message = "<div>确定要启用应用: <span>&nbsp;&nbsp;&nbsp;" + row.name + " ?</span></div>"+
          "<span style='margin-top: 20px'>应用ID:</span> &nbsp;&nbsp;&nbsp;" + row.uid
      }else{
        message = "<div>确定要停用应用: <span>" + row.name + " ?</span></div>"+
          "<span style='margin-top: 20px'>应用ID:</span> &nbsp;&nbsp;&nbsp;" + row.uid
      }
      this.$modal.confirm(message).then(function() {
        return updateSpeakerInfoStatus(data);
      }).then(() => {
        if (row.status == 0) {
          this.$modal.msgSuccess("已启用");
        } else {
          this.$modal.msgSuccess("已停用");
        }
        this.getList();
      }).catch(() => {});
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.speakerTypeList = []
      this.form = {};
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加发音人";
      let obj = {status : 0};
      this.form = obj;
      this.selectSpeakerInfoList();
    },
    selectSpeakerInfoList(){
      //参数直接默认查询1000条
      let param = {};
      param.pageNum = 1;
      param.pageSize = 1000;
      param.type=0
      listSpeakerTypeInfo(param).then(response =>{
        this.speakerTypeList = response.rows
      })
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      this.open = true;
      this.title = "编辑发音人";
      this.selectSpeakerInfoList();
      selectSpeakerInfo(row.uid).then(response =>{
        this.form = response.data;
        if(this.form.param != null){
          this.form.param = JSON.stringify(JSON.parse(this.form.param), null, 2);
        }
      })

    },
    /** 提交按钮 */
    submitForm: function() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.uid == undefined) {
            insertSpeakerInfo(this.form).then(response => {
              this.$modal.msgSuccess("添加成功");
              this.open = false;
              this.getList();
            })
          }else{
            let data = this.form;
            //提示
            const message = "<div>确定要修改配置信息？</div>"
            this.$modal.confirm(message).then(function() {
              return updateSpeakerInfo(data);
            }).then(() => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            })
          }
        }
      })
    },

    /** 删除按钮操作 */
    handleDelete(row) {
      let data = {}
      data.uid = row.uid;
      //提示
      const message = "<div>确定要删除应用: <span>" + row.name + " ?</span></div>"+
        "<span style='margin-top: 20px'>应用ID:</span> &nbsp;&nbsp;&nbsp;" + row.uid
      this.$modal.confirm(message).then(function() {
        return deleteSpeakerInfo(data);
      }).then(() => {
        this.$modal.msgSuccess("删除成功");
        this.getList()
      })
    },
  }
};
</script>

<style scoped>
/deep/ .el-textarea__inner {
  resize: none;
}
.sound_info{
  background: rgba(240, 240, 240, 0.5);
  filter: Alpha(opacity=20);
}
</style>
